const { default: api } = require("../../../utils/api");
const message = require("../../../utils/message");

// pages/retail/extract/extract.js
Page({

    /**
     * 页面的初始数据
     */
    data: {

        retail_id:null,
        has_load:false,
        extract:null,
        extract_amount:0.00,
        bank_id:-1,
        bank_list:[]

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(options.id){

            this.setData({
                retail_id: options.id
            })

            this.getRetailUserExtractInfo();
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getRetailUserExtractInfo:function () {
        var me = this;
        var params = {
        }

        params.retail_id = this.data.retail_id;

        message.show_loading()
        api.retail.getRetailUserExtractInfo( params , function (response) {
            message.hide_loading();
            if(response.status==1){

                me.setData({
                    has_load:true,
                    extract: response.data.extract,
                    bank_list: response.data.bank_list

                })
            }else{
                message.show_error( response );
            }
        })
    },
    doExtractAll:function ( ) {

        var extract = this.data.extract;
        var amount =  extract.extract_amount;
        this.setData({
            extract_amount:  amount
        })
    },
    bindInputContent:function (e) {

        var value = e.detail.value;
        
        this.setData({
            extract_amount:e.detail.value
        })
    },
    selectBank:function (e) {
        var bank_id = e.currentTarget.dataset.id;

        this.setData({
            bank_id:bank_id
        })
        
    },
    doRetailExtract:function (params) {
        var me = this;

        if(this.data.bank_id<=0){
            message.show_error("请选择要收款的账户");
            return;
        }
        if(this.data.extract_amount<0){
            message.show_error("提现金额不得少于50元");
            return;
        }

        
        var params = {}

        params.bank_id = this.data.bank_id;
        params.extract_amount = this.data.extract_amount;
        params.retail_id = this.data.retail_id;

        api.retail.doRetailExtract( params , function (response) {
            if(response.status==1){
                message.show_success( response.message , function () {
                    me.getRetailUserExtractInfo();
                })
            }else{
                message.show_error( response.message )
            }
            
        } ,"json")


        
    }
})